package org.grottarossa.template.data.enums;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;


public enum EnumDataType
{
	Default(0),
	Boolean(1),
	Integer(2),
	Duration(3),
	String(4),
	Calendar(5),
	BigDecimal(6),
	LongText(11);

	public static Object convertType(EnumDataType type, String value)
	{
		Class keyType = getJavaType(type);

		if(keyType == Integer.class)
			return java.lang.Integer.parseInt(value);
		else if(keyType == Boolean.class)
			return java.lang.Boolean.parseBoolean(value);
		else if(keyType == String.class)
			return value;
		else if(keyType == Calendar.class)
			return parseTimestamp(value);
		else
			throw new IllegalArgumentException("Type not supported");
	}

	private static Calendar parseTimestamp(String timestamp) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy",
					Locale.ITALY);
			Date d;

			d = sdf.parse(timestamp);

			Calendar cal = java.util.Calendar.getInstance();
			cal.setTime(d);
			return cal;
		} catch (ParseException e) {
			return null;
		}
	}

	public static Class getJavaType(EnumDataType type)
	{
		switch (type)
		{
		case Boolean:
			return Boolean.class;
		case Integer:
			return Integer.class;
		case String:
		case LongText:
			return String.class;
		case BigDecimal:
			return BigDecimal.class;
		case Calendar:
			return Calendar.class;
		default:
			return Integer.class;
		}
	}

	EnumDataType(int val)
	{}
	public static EnumDataType getEnumValue(int target)
	{
		switch (target)
		{
		case 0:
			return Default;
		case 1:
			return Boolean;
		case 2:
			return Integer;
		case 3:
			return Duration;
		case 4:
			return String;
		case 5:
			return Calendar;
		case 6:
			return BigDecimal;
		case 11:
			return LongText;
		default:
			return Default;
		}
	}

	public boolean isDefault()
	{
		switch (this) {
		case Default:
			return true;
		}
		return false;
	}

	public boolean isBoolean()
	{
		switch (this) {
		case Boolean:
			return true;
		}
		return false;
	}
	public boolean isInteger()
	{
		switch (this) {
		case Integer:
			return true;
		}
		return false;
	}
	public boolean isDuration()
	{
		switch (this) {
		case Duration:
			return true;
		}
		return false;
	}
	public boolean isString()
	{
		switch (this) {
		case String:
			return true;
		}
		return false;
	}

	public boolean isCalendar()
	{
		switch (this) {
		case Calendar:
			return true;
		}
		return false;
	}


	public boolean isBigDecimal()
	{
		switch (this) {
		case BigDecimal:
			return true;
		}
		return false;
	}

	public boolean isLongText()
	{
		switch (this) {
		case LongText:
			return true;
		}
		return false;
	}


}